package com.moon.game.command.guest;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.exception.GException;
import com.moon.game.manager.GiftMG;
import com.moon.game.manager.LevelExpMG;
import com.moon.game.manager.MainLandMG;
import com.moon.game.manager.NoticeMG;
import com.moon.game.resource.model.Gift;
import com.moon.game.resource.model.Monster;
import com.moon.game.resource.model.Notice;
import com.moon.game.resource.model.Npcs;
import com.moon.game.service.ChatService;
import com.moon.game.service.FightService;
import com.moon.game.service.MainLandService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.GiftVo;
import com.moon.game.vos.MsgVo;
import com.moon.game.vos.PlayerVo;

/**
 * @description 查看主界面 
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-27 下午2:00:10
 */
public class ViewMain extends BaseCmd{

	private static final Logger log = Logger.getLogger(ViewMain.class);
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		PlayerVo player = getPlayerVo(req);
		req.setVO(PAR_SID, player.getSid());
		req.setVO(PAR_LEVEL, player.getLevel());
		req.setVO(PAR_NAME, player.getName());
		req.setVO(PAR_CHARM_VALUE, player.getCharmValue());
		req.setVO(PAR_EXP, player.getExp());
		req.setVO(PAR_FULL_EXP, LevelExpMG.instance.getLevelForExp(player.getLevel()));
		req.setVO("cur_hp", player.getCurHp());
		req.setVO("hp", player.getHp());
		req.setVO("cur_mp", player.getCurMp());
		req.setVO("mp", player.getMp());
		req.setVO("location", player.getLocation());
		if (player.getCurHp() <= 0) {
			PlayerService.doAddHp(player.getSid(), player.getId(), (int)(player.getHp() * 0.05));
		}
		boolean isInCity = true;
		if (MainLandMG.instance.getMainLand(player.getLocation()).getLevel() > 0) {
			ArrayList<Monster> monsterList = FightService.findMonster(player);
			req.setVO("monster_list", monsterList);
			isInCity = false;
		}
		List<GiftVo> gifts = player.getGiftList();
		boolean hasCanOpenedGift = false;
		if (gifts != null && gifts.size() > 0) {
			for (GiftVo vo : gifts) {
				Gift gift = GiftMG.instance.getGiftById(vo.getGiftId());
				if (gift != null && gift.getLevel() <= player.getLevel()) {
					hasCanOpenedGift = true;
					break;
				}
			}
		}
		req.setVO("has_can_opened_gift", hasCanOpenedGift);
		final ArrayList<Npcs> npcList = MainLandService.getNpcs(player.getLocation());
		ArrayList<Npcs> npcs = new ArrayList<Npcs>();
		if (npcList != null) {
			for (Npcs npc : npcList) {
				npcs.add(npc);
			}
		}
		req.setVO("in_city", isInCity);
		req.setVO("npc_list", npcs);
//		player.setLevel(40);
		PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
		LinkedList<MsgVo> chatList = ChatService.getWorldChatList();
		LinkedList<MsgVo> showChat = new LinkedList<MsgVo>();
		if (chatList != null) {
			int size = 0;
			for (MsgVo vo : chatList) {
				showChat.addLast(vo);
				if (++size > 1) {
					break;
				}
			}
		}
		req.setVO("show_chat", showChat);
		LinkedList<MsgVo> privateChatList = ChatService.getPrivateMessage(req.getPlayerId());
		LinkedList<MsgVo> showPrivateChat = new LinkedList<MsgVo>();
		if (privateChatList != null) {
			int size = 0;
			for (MsgVo vo : privateChatList) {
				showPrivateChat.addLast(vo);
				if (++size > 1) {
					break;
				}
			}
		}
		req.setVO("show_private_chat", showPrivateChat);
		
		LinkedList<MsgVo> privateActChatList = ChatService.getPrivateActMessage(req.getPlayerId());
		LinkedList<MsgVo> showActPrivateChat = new LinkedList<MsgVo>();
		if (privateActChatList != null) {
			int size = 0;
			for (MsgVo vo : privateActChatList) {
				showActPrivateChat.addLast(vo);
				if (++size > 1) {
					break;
				}
			}
		}
		req.setVO("show_private_act_chat", showActPrivateChat);
		
		//公告
		List<Notice> notices = NoticeMG.instance.getNotices();
		if (notices != null && notices.size() > 1) {
			Notice notice = notices.get(new Random().nextInt(notices.size()));
			Notice randNotice = new Notice();
			randNotice.setId(notice.getId());
			randNotice.setName(notice.getName());
			req.setVO("sys_notice", randNotice);
		}
		response(req, req.getClientResponse());
	}
	
}
